package com.lotus.lqmp.service.biz.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lotus.idaas.sdk.common.UserInfoContext;
import com.lotus.lqmp.base.biz.bean.PageQuery;
import com.lotus.lqmp.base.third.model.CommonUserDTO;
import com.lotus.lqmp.service.biz.BizModifyRecordBizService;
import com.lotus.lqmp.service.common.convert.BizModifyRecordConvert;
import com.lotus.lqmp.service.common.enums.RecordBizOperateEnum;
import com.lotus.lqmp.service.common.enums.RecordBizTypeEnum;
import com.lotus.lqmp.service.controller.vo.req.BizModifyRecordQueryReq;
import com.lotus.lqmp.service.controller.vo.resp.BizModifyRecordQueryResp;
import com.lotus.lqmp.service.dao.entity.BizModifyRecordDo;
import com.lotus.lqmp.service.service.BizModifyRecordService;
import com.lotus.lqmp.service.service.UserInfoTransferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author Shuang.Pan2
 * description
 * @since 2022/03/31 13:12
 */
@Service
public class BizModifyRecordBizServiceImpl implements BizModifyRecordBizService {

    @Autowired
    private BizModifyRecordService bizModifyRecordService;

    @Autowired
    private BizModifyRecordConvert bizModifyRecordConvert;

    @Autowired
    private UserInfoTransferService userInfoTransferService;

    @Override
    public void saveModifyRecord(RecordBizOperateEnum recordBizOperateEnum, String modifyJson, String modifyReason,
        RecordBizTypeEnum recordBizTypeEnum) {
        BizModifyRecordDo bizModifyRecordDo = new BizModifyRecordDo();
        bizModifyRecordDo.setModifyReason(modifyReason);
        bizModifyRecordDo.setBizType(recordBizTypeEnum.name());
        bizModifyRecordDo.setCreator(UserInfoContext.getUserInfo().getEmployeeNo());
        bizModifyRecordDo.setEditor(UserInfoContext.getUserInfo().getEmployeeNo());
        bizModifyRecordDo.setOperationType(recordBizOperateEnum.getOperateType());
        bizModifyRecordDo.setOperationContent(modifyJson);
        bizModifyRecordService.save(bizModifyRecordDo);
    }

    @Override
    public Page<BizModifyRecordQueryResp> queryForPage(PageQuery<BizModifyRecordQueryReq> queryInfo) {
        Page<BizModifyRecordDo> doPage = bizModifyRecordService.queryForPage(queryInfo);
        Page<BizModifyRecordQueryResp> respPage = new Page<>(queryInfo.getPageNum(), queryInfo.getPageSize());
        respPage.setTotal(doPage.getTotal());
        Map<String, CommonUserDTO> no2UserInfo = userInfoTransferService.getUserInfoByEmployeeNos(
            doPage.getRecords().stream().map(BizModifyRecordDo::getCreator).collect(Collectors.toList()));
        Map<Long, BizModifyRecordDo> id2Do = doPage.getRecords().stream()
            .collect(Collectors.toMap(BizModifyRecordDo::getId, Function.identity()));
        respPage.setRecords(bizModifyRecordConvert.dos2Resps(doPage.getRecords()));
        respPage.getRecords().forEach(curr -> {
            curr.setCreator(no2UserInfo.get(id2Do.get(curr.getId()).getCreator()));
        });
        return respPage;
    }
}
